package org.dromara.timemate.service.impl;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.timemate.domain.DeviceInfo;
import org.dromara.timemate.domain.bo.DeviceManageBo;
import org.dromara.timemate.domain.vo.DeviceManageVo;
import org.dromara.timemate.mapper.DeviceInfoMapper;
import org.dromara.timemate.service.DeviceManageService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class DeviceManageServiceImpl implements DeviceManageService {
    @Resource
    private DeviceInfoMapper deviceInfoMapper;
    private final SysUserMapper userMapper;

    @Override
    public TableDataInfo<DeviceManageVo> queryPageList(DeviceManageBo bo, PageQuery pageQuery) {
        String phonenumber = bo.getPhonenumber();
        String deviceType = bo.getDeviceType();
        Page<DeviceManageVo> page = pageQuery.build();
        Integer pageNum = (int) page.getCurrent();
        Integer pageSize = (int) page.getSize();
        Integer start = (pageNum - 1) * pageSize;
        List<DeviceManageVo> deviceManageVos = deviceInfoMapper.ByPhoneDTypePageQuery(phonenumber, deviceType, start, pageSize);
        long  total= deviceInfoMapper.ByPhoneDTypeSelectCount(phonenumber, deviceType);
        TableDataInfo tableDataInfo = new TableDataInfo(deviceManageVos, total);
        tableDataInfo.setCode(HttpStatus.HTTP_OK);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    @Override
    public Boolean addDeviceManageInfo(DeviceManageBo deviceManageBo) {
        String tenantId = LoginHelper.getTenantId();
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceType(deviceManageBo.getDeviceType());
        deviceInfo.setUserId(deviceManageBo.getUserId());
        deviceInfo.setWearerId(deviceManageBo.getWearerId());
        deviceInfo.setDeviceName(deviceManageBo.getDeviceName());
        deviceInfo.setTenantId(tenantId);
        if (deviceInfoMapper.insert(deviceInfo) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<DeviceManageVo> queryByTenantId() {
        String tenantId = LoginHelper.getTenantId();
        LambdaQueryWrapper<SysUser> Wrapper = new LambdaQueryWrapper<SysUser>().eq(SysUser::getTenantId, tenantId);
        List<SysUser> sysUsers = userMapper.selectList(Wrapper);
        List<DeviceManageVo> DeviceManageVo = sysUsers.stream().map(user -> new DeviceManageVo(user.getUserId(), user.getPhonenumber())).collect(Collectors.toList());
        return DeviceManageVo;
    }

    @Override
    public Boolean updateByBo(DeviceManageBo bo) {
        DeviceInfo deviceInfo = MapstructUtils.convert(bo, DeviceInfo.class);
        return deviceInfoMapper.updateById(deviceInfo)>0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids) {
        return deviceInfoMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<DeviceManageVo> queryList(DeviceManageBo bo) {
        String phonenumber = bo.getPhonenumber();
        String deviceType = bo.getDeviceType();
        List<DeviceManageVo> deviceManageVos = deviceInfoMapper.queryList(phonenumber, deviceType);
        return deviceManageVos;
    }

    @Override
    public Boolean saveBatch(List<DeviceInfo> list) {
        return deviceInfoMapper.insertBatch(list);
    }


}

